// pages/share/index.js
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    url: '',
    urlData: {},
    type: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    this.setData({
      urlData: options
    })
    if (app.globalData.userInfo) {
      if (app.globalData.registerInfo) {
        wx.showLoading({
          title: '页面加载中...',
        })
        this.setData({
          url: `${app.globalData.traceUrl}?type=${options.type}&share=${options.share}&deptId=${options.deptId}&deptName=${options.deptName}&userInfo=${encodeURI(JSON.stringify(app.globalData.userInfo))}&registerInfo=${
            encodeURI(JSON.stringify(app.globalData.registerInfo))}`,
          type: options.type
        })
        setTimeout(() => {
          wx.hideLoading()
        }, 2000)
      } else {
        app.accountCallback = res => {
          wx.showLoading({
            title: '页面加载中...',
          })
          this.setData({
            url: `${app.globalData.traceUrl}?type=${options.type}&share=${options.share}&deptId=${options.deptId}&deptName=${options.deptName}&userInfo=${encodeURI(JSON.stringify(app.globalData.userInfo))}&registerInfo=${
              encodeURI(JSON.stringify(app.globalData.registerInfo))}`,
            type: options.type
          })
          setTimeout(() => {
            wx.hideLoading()
          }, 2000)
        }
        app.userLogin()
      }
    } else {
      wx.reLaunch({
        url: '/pages/authorize/index'
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.hideHomeButton()
    var that = this
    wx.setStorage({
      key: "addEmployee",
      data: {
        type: that.data.type,
        no: "1"
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  h5PostMessage: function (e) {
    console.log(e.detail.data)
    var that = this
    wx.setStorage({
      key: "addEmployee",
      data: {
        isGoBackHome: e.detail.data[0],
        type: that.data.type,
        no: "1"
      }
    })
    this.h5Data = e.detail.data[e.detail.data.length - 1]; //当用户点击这个h5页面的分享时获取到
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (options) {
    return {
      title: '人员添加',
      desc: '助力企业复工复产',
      path: `/pages/share/index?type=${this.data.type}&share=1&deptId=${this.data.urlData.deptId}&deptName=${this.data.urlData.deptName}`
    }
  },
  h5Load: function () {
    wx.hideLoading()
  },
  h5LoadError: function (e) {
    wx.hideLoading()
    wx.showToast({
      title: e.detail,
      duration: 2000
    })
  }
})
